package jcode.code;

import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import org.junit.Before;
import org.junit.Test;
import jcode.code.jframe.gen;

public class te {






    

    public <T> T mapToObj(Map<String, Object> map, Class<T> clz) throws Exception {
        T obj = clz.getDeclaredConstructor().newInstance();

        Field[] fields = clz.getDeclaredFields();

        map.forEach((k, v) -> {
            try {
                Optional<Field> optional = Arrays.stream(fields).filter(f -> k.equals(f.getName())).findFirst();
                if (optional.isPresent()) {
                    Field field = optional.get();
                    field.setAccessible(true);
                    field.set(obj, v);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        return obj;
    }


    


   @Test
    public void testFile(){
        File file = new File("C:\\Users");
        File[] files = file.listFiles();
        System.out.println(Arrays.toString(files));

        File[] files1 = file.listFiles(File::isDirectory);
        File[] files2 = file.listFiles(File::isFile);

        System.out.println(Arrays.toString(files1));
        System.out.println(Arrays.toString(files2));

        File[] files3 = file.listFiles(f -> f.isFile() && f.getName().endsWith(".java"));
        System.out.println(Arrays.toString(files3));

    }
/*                  SPARSEMATRIX
*
*
*/
    static int[][] ars;
    static {
        ars = new int[15][15];
        ars[4][4] = 11;
        ars[4][5] = 1;
        ars[4][6] = 1;
        ars[4][7] = 1;
        ars[10][8] = 1;   
    }
    private static String toString(int[][] ars){
        StringBuilder ret = new StringBuilder();
        for (int[] rows: ars){
            for (int num: rows){
                ret.append(num +"\t") ;
            }
            ret.append("\r\n");
            }
        return ret.toString();
        }
    
    private static int[][] convertArray(int[][] sparseMatrix){
        int[][] arrs = new int[sparseMatrix[0][0]][sparseMatrix[0][1]];

        for (int i =1; i < sparseMatrix.length; i++) {
            arrs[sparseMatrix[i][0]][sparseMatrix[i][1]] = sparseMatrix[i][2];
        }
        return arrs;
    }

    private static int[][] convertSparseMatrix(int[][] ars){

        int noZeroSum = 0;
        for(int[] rows : ars){
            for (int num: rows){
                if (num != 0)
                    noZeroSum++;
            }
        }

        int[][] sparseMatrix = new int[noZeroSum+1][3];
        sparseMatrix[0][0] = ars.length;
        sparseMatrix[0][1] = ars.length;
        sparseMatrix[0][2] = noZeroSum;

        int noZeroRow = 0;
        for (int i = 0; i<ars.length; i++){
            for (int j = 0; j<ars[i].length; j++){
                if (ars[i][j] != 0){
                    noZeroRow++;
                    sparseMatrix[noZeroRow][0] = i;
                    sparseMatrix[noZeroRow][1] = j;
                    sparseMatrix[noZeroRow][2] = ars[i][j];
                }    
            }
        }
        return sparseMatrix;
    }
    
    @Test
    public void testSparseMatrix(){
        System.out.println(toString(ars));
        int[][] sparseMatrix = convertSparseMatrix(ars);
         
        System.out.println(toString(sparseMatrix));

        int[][] arrs = convertArray(sparseMatrix);
        System.out.println(toString(arrs));
    }
/*
* sparseMatrix
*
*/
    @Test
    public void test5() {

        
        Set<Integer> set = new ConcurrentSkipListSet<>();

        set.add(1);
        set.add(33);
        set.add(2);
        set.add(56);

        set.forEach(System.out::println);
    }

    @Test
    public void test44() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "zhangsan");
        map.put("age", 18);
        
    }

    @Test
    public void test4() throws InterruptedException {

        ArrayBlockingQueue<String> abq = new ArrayBlockingQueue<>(5, false);
    
        new Thread(() -> {
            while (true) {
                try {
                    System.out.println("take data " + abq.poll());
                } catch (Exception e) {
                    System.out.println("error");
                }
            }

        }).start();

        Thread.sleep(3000L);

        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                try {
                    abq.put(Thread.currentThread().getName());
                    System.out.println("put data " + Thread.currentThread());
                } catch (InterruptedException e) {
                }
            }).start();
        }

        new Thread(() -> {
            for(int i = 0; i < 4; i++) {
                try {
                    System.out.println("take data " + abq.poll());
                } catch (Exception e) {
                    System.out.println("error");
                }
            }

        }).start();

        Properties properties = new Properties();

        System.out.println(properties);
    }

    @Test
    public void test3() throws Exception {
        Class<Person> clz = Person.class;

        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName());
            System.out.println(field.getModifiers());
            System.out.printf("%s \n", field.getType());
        }

        Constructor<Person> declaredConstructor = clz.getDeclaredConstructor();
        Person person = declaredConstructor.newInstance();

        System.out.println(person);
    }

    ArrayList<Map<String, Object>> list1 = new ArrayList<>();

    @Before
    public void before() {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "te");
        map.put("c", 99);
        map.put("s", 11);

        list1.add(map);
    }

    private static final int _2 = 2;
    ArrayList<Integer> list = new ArrayList<Integer>();

    @Test
    public void testSG() {
        gen.call(_2);

        // method reference ::
        Consumer<String> consumer = System.out::println;
        consumer.accept("ff");

        Supplier<Person> supplier = Person::new; // ()->new Person("cc", 111);
        test(supplier);
    }

    public void test(Supplier<Person> supplier) {
        ArrayList<Person> list = new ArrayList<Person>();
        list.add(supplier.get());
        System.out.println(list);
    }

}